from apps.model.serializeModel import model_to_list, model_to_dict
from apps.settingApi.switchCase.serializeParameter import pageResultData, serializeGetFilter, serializeParData
from comm.sqlalchemyConfig import db


def getList(basicDataViewModel,args):
    # 格式化查询参数
    serializeFilterList, args = serializeGetFilter(basicDataViewModel,args)
    # 分页查询数据
    modelDataObj = basicDataViewModel.query.filter(*serializeFilterList).order_by(basicDataViewModel.id.desc()).paginate(page=args['page'],
                                                                                  per_page=args['pageSize'],
                                                                                  error_out=False)
    # 格式化分页数据
    objData = pageResultData(modelDataObj)
    # 格式化数据列表
    if args['basic_value'] == 'dbpool':
        excludeFiledList = ['str01']
    else:
        excludeFiledList = []
    result = model_to_list(modelDataObj, excludeFiledList)
    for itm in result:
        itm["print_qty"] = 1
    # 拼接返回的数据
    objData["data"] = result
    return objData

def postList(basicDataViewModel,args):
    # 序列化跟校验参数
    addData, err = serializeParData(model=basicDataViewModel, argsData=args)
    if err != None:
        return None,err
    # 判断编码是否重复
    exit_z = db.session.query(basicDataViewModel).filter(
        basicDataViewModel.lessees_id == args['lessees_id'],
        basicDataViewModel.basic_value == args['basic_value'],
        basicDataViewModel.value == args['value']).first()
    if exit_z != None:
        return None,'编码重复'
    result = db.session.query(basicDataViewModel).with_for_update().filter_by(id=args['id']).first()
    # 如果数据不存在，则添加新数据
    if result != None:
        db.session.commit()
        return None,'抢先操作重新提交试试'
    try:
        add = basicDataViewModel(**addData)
        db.session.add(add)
        db.session.commit()
        db.session.flush()
        objData = model_to_dict(add, [])
    except Exception as e:
        return None,repr(e)
    return objData,None

def getpk(basicDataViewModel,args,pk):
    # 查询数据
    result = basicDataViewModel.query.filter(
         basicDataViewModel.lessees_id == args['lessees_id'],
         basicDataViewModel.basic_value == args['basic_value'],
         basicDataViewModel.id == pk).first()
    if result == None:
        return None,'没有查询到数据'
    if args['basic_value'] == 'dbpool':
        excludeFiledList = ['str01']
    else:
        excludeFiledList = []
    resultData = model_to_dict(result, excludeFiledList)
    return resultData,None

def putPk(basicDataViewModel,args,pk):
    # 序列化跟校验参数
    addData, err = serializeParData(model=basicDataViewModel, argsData=args)
    if err != None:
        return None,err
    # 判断物料是否存在
    exit_z = db.session.query(basicDataViewModel).filter(
         basicDataViewModel.lessees_id == args['lessees_id'],
         basicDataViewModel.basic_value == args['basic_value'],
         basicDataViewModel.id == pk
         ).first()
    if exit_z is None:
        return None, '资料不存在'
    # 判断编码是否重复
    exit_z = db.session.query(basicDataViewModel).filter(
         basicDataViewModel.lessees_id == args['lessees_id'],
         basicDataViewModel.basic_value == args['basic_value'],
         basicDataViewModel.id != pk,
         basicDataViewModel.value == args['value'],
         ).first()
    if exit_z != None:
        return None,'编码重复'
    # 修改数据
    try:
        addr = basicDataViewModel.query.filter(basicDataViewModel.id == pk).with_for_update().first()
        for itm in addData.keys():
            setattr(addr, itm, addData[itm])
        db.session.commit()
    except Exception as e:
        return None, repr(e)
    # 查询修改后的数据进行返回
    update = basicDataViewModel.query.filter(basicDataViewModel.id == pk).first()
    if update is None:
        return None, '获取更新后的数据失败'
    objData = model_to_dict(update, [])
    return objData,None

def deletePk(basicDataViewModel,args,pk):
    # 判断数据是否存在
    exit_z = db.session.query(basicDataViewModel).filter(
         basicDataViewModel.lessees_id == args['lessees_id'],
         basicDataViewModel.basic_value == args['basic_value'],
         basicDataViewModel.id == pk
         ).first()
    if exit_z is None:
        return None,'资料不存在'
    # 删除数据
    try:
        deletedata = db.session.query(basicDataViewModel).filter(
            basicDataViewModel.id == pk,
            basicDataViewModel.lessees_id==args['lessees_id']
            ).first()
        db.session.delete(deletedata)
        db.session.commit()
    except Exception as e:
        return None,'删除失败' + repr(e)
    # 返回删除的数据
    objData = model_to_dict(exit_z, [])
    return objData,None
